﻿
using Red.Core;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace Red.Controls
{
    /// <summary>
    ///     Button with icon
    /// </summary>
    public class RedButton : Button
    {
        #region Properties
        
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register( "Icon", typeof( Canvas ), typeof( RedButton ) );

        /// <summary>
        ///     Icon displayed on button
        /// </summary>
        public Canvas Icon
        {
            get { return (Canvas)GetValue( IconProperty ); }
            set { SetValue( IconProperty, value ); }
        }

        #endregion Properties

        public RedButton()
        {
            var icon = new RedIcon();
            icon.SetBinding( RedIcon.CanvasProperty, new Binding( "Icon" ) { Source = this } );
            AddChild( icon );
        }

        #region Event handlers
        
        protected override void OnPropertyChanged( DependencyPropertyChangedEventArgs e )
        {
            base.OnPropertyChanged( e );

            if ( e.Property == CommandProperty )
            {
                var cmd = Command as RoutedUICommand;
                if ( cmd != null )
                {
                    ToolTip = cmd.Text;
                }

                if ( Icon == null && cmd is RedCommand )
                {
                    Icon = ( (RedCommand)cmd ).Icon;
                }
            }
        }
        
        #endregion Event handlers
    }
        
}
